package com.woniuxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.config.RedisConfig;
import com.woniuxy.domain.Seat;
import com.woniuxy.domain.Ticket;
import com.woniuxy.dto.JsonResultDto;
import com.woniuxy.mapper.TicketMapper;
import com.woniuxy.param.SeatXYParam;
import com.woniuxy.service.TicketService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wuyanzu
 * @since 2021-05-26
 */
@Service
public class TicketServiceImpl extends ServiceImpl<TicketMapper, Ticket> implements TicketService {

    @Autowired
    private RedisConfig redisConfig;
    @Override
    public Boolean insertTicketBySeat(Seat seat) {
        Ticket ticket = new Ticket();
        BeanUtils.copyProperties(seat,ticket);
        String code = "";

        for(int i =0;i<6;i++){
           code += String.valueOf((int)(Math.random()*10));
        }
        ticket.setTicketCode(code);
        boolean save = this.save(ticket);
        redisConfig.opsForHash().put("Ticket",ticket.gettId(),ticket);
        return save;
    }
    //座位编号对象(seatParam)
    public JsonResultDto selectValidTicketByXY(SeatXYParam seatXYParam){
        QueryWrapper<Ticket> wrapper = new QueryWrapper<>();
        wrapper.eq("x",seatXYParam.getX());
        wrapper.eq("y",seatXYParam.getY());
        Ticket ticket = this.getOne(wrapper);
        System.out.println(ticket.gettStatus());
        if(ticket.gettStatus().equals("0")){
            JsonResultDto<Ticket> builder = JsonResultDto.builder(Ticket.class);
            builder.setCode("1100");
            builder.setObj(ticket);
            builder.setMsg("此位置已被订购");
            return builder;
        }else{
            return new JsonResultDto("1101","此位置可订购",ticket,null);
        }
    }

    //根据产生订单(待付款)ticket对象更改其中的数据
    public boolean changeTicketOrder(Ticket ticket){
        ticket.settStatus("0");
        return this.updateById(ticket);
    }
}
